package com.along.textdemo;

import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.along.textdemo.base.BaseActivity;
import com.along.textdemo.databinding.ActivityMainBinding;
import com.along.textdemo.model.MainViewModel;
import com.along.textdemo.utils.ScreenUtils;
import com.along.textdemo.view.AudioRecorderButton;

public class MainActivity extends BaseActivity<MainViewModel,ActivityMainBinding> implements View.OnTouchListener {


    private final String TAG = "MainActivity";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void processLogic() {
        //去进度条界面
        binding.btnToProgress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,ProgressViewActivity.class));
            }
        });
        //去语音界面
        binding.btnToVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,VoiceActivity.class));
            }
        });
        //去声纹界面
        binding.btnToVoicePrint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,VoicePrintActivity.class));
            }
        });

        //去声纹对比界面
        binding.btnToVoicePrintCompare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,VoicePrintCompareActivity.class));
            }
        });
        //去声纹界面,第二版
        binding.btnToVoicePrint2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,VoicePrint2Activity.class));
            }
        });
        //去声纹对比界面,第二版
        binding.btnToVoicePrintCompare2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,VoicePrint2Activity.class));
            }
        });

        //使用自定义button，去实现录音和弹窗内容切换
//        binding.btnVoice.setAudioFinishRecorderListener(new AudioRecorderButton.AudioFinishRecorderListener() {
//            @Override
//            public void onFinish(float time, String filePath) {
//
//            }
//        });

        //简单实现，弹窗的内容
        binding.tvGetResult.setOnTouchListener(this);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.tv_getResult) {
            float startY = 0;
            float endY = 0;
            boolean send = false;
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    startY = event.getY();
                    Log.e(TAG, "audioButtonDown() MotionEvent.ACTION_DOWN");
                    showAudioDialog();
                    break;
                case MotionEvent.ACTION_UP:
                    endY = event.getY();
                    Log.e(TAG, "audioButtonDown() MotionEvent.ACTION_UP");
                    endActonUp();
                    send = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    float moveY = event.getY();
                    int instance = (int) Math.abs((moveY - startY));
                    Log.e(TAG, "--action move--instance:"+instance);
                    if (instance > 100) {
                        changeAudioDialogCancel(true);
                    } else {
                        changeAudioDialogCancel(false);
                    }
                    break;
            }
            return true;
        }
        return false;
    }

    private Dialog mDialog;
    ImageView mIcon;
    ImageView mVoice;
    TextView mLabel;
    LinearLayout mImagelayout,mBtnlayout;
    public void showAudioDialog() {
        mDialog = new Dialog(this, R.style.Theme_AudioDialog);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.dialog, null);
        mDialog.setContentView(view);

        mImagelayout = (LinearLayout) mDialog.findViewById(R.id.image_layout);
        mBtnlayout= (LinearLayout) mDialog.findViewById(R.id.btn_layout);
        mIcon = (ImageView) mDialog.findViewById(R.id.main_recorder_dialog_icon);
        mVoice = (ImageView) mDialog.findViewById(R.id.main_recorder_dialog_voice);
        mLabel = (TextView) mDialog.findViewById(R.id.main_recorder_dialog_label);

        Window window = mDialog.getWindow();

        window.setGravity(Gravity.CENTER);

        int width = ScreenUtils.getScreenWidth(this);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int)(width*0.8);
//        lp.y = 400;  //设置出现的高度，距离底部
        window.setAttributes(lp);

        mDialog.show();
    }

    private void hideAudioDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }
    private boolean mCancelSend;
    private void changeAudioDialogCancel(boolean cancel) {
        if (mCancelSend == cancel)
            return;
        if (mDialog != null && mDialog.isShowing()
                && mLabel != null) {
            if (cancel) {
                mLabel.setText(R.string.str_recorder_recording);
            } else {
                mLabel.setText(R.string.str_recorder_want_cancel);
            }
        }
        mCancelSend = cancel;
    }

    private void endActonUp(){
        if (mDialog != null && mDialog.isShowing()
                && mImagelayout != null) {
            mImagelayout.setVisibility(View.GONE);
            mBtnlayout.setVisibility(View.VISIBLE);

            mLabel.setText("我在这里发了语音");
        }
    }

}
